﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace MINIGKP
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        public string ddiD = "";
        public static bool ismake = false;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //SpeechVoice.Instance.VoicePlayback("Please take your drink");
                // 绑定视频文件
                myMediaElement.Source = new Uri(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "video.mp4");
                // 交互式控制
                myMediaElement.LoadedBehavior = MediaState.Manual;
                // 添加元素加载完成事件 -- 自动开始播放
                myMediaElement.Loaded += new RoutedEventHandler(media_Loaded);
                // 添加媒体播放结束事件 -- 重新播放
                myMediaElement.MediaEnded += new RoutedEventHandler(media_MediaEnded);
                // 添加元素卸载完成事件 -- 停止播放
                myMediaElement.Unloaded += new RoutedEventHandler(media_Unloaded);
                OrderShowClear();
                CoffeeMsgClear();
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.SMPortName = porPortInfo.SMPortName;
                    IPorPortInfo.Instance.IOPortName = porPortInfo.IOPortName;
                    IPorPortInfo.Instance.SweetInit1 = porPortInfo.SweetInit1;
                    IPorPortInfo.Instance.SweetInit2 = porPortInfo.SweetInit2;
                    IPorPortInfo.Instance.SweetInit3 = porPortInfo.SweetInit3;
                }
                Thread thr3 = new Thread(SocketClients.Instance.socketclient);
                thr3.IsBackground = true;
                thr3.Start();
                Thread thr1 = new Thread(MessageShow);
                thr1.IsBackground = true;
                thr1.Start();
                //System.Timers.Timer timer = new System.Timers.Timer();
                //timer.Interval = 2000;
                //timer.Elapsed += Timer_Elapsed;
                //timer.Start();
                this.Left = 1920;
                this.Top = -10;
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 单个咖啡状态信息显示
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="guige"></param>
        /// <param name="price"></param>
        /// <param name="img"></param>
        public void CoffeeTypeShow(string goodsid, int id, string name, string status, string guige, string price, string img)
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(delegate
                {
                    if (id == 0)
                    {
                        coffeeName1.Text = name;
                        coffee1.Text = status == "3" ? "To be made" : status == "4" ? MainWindow.ismake ? "In Process" : "In Process" : "Completed";
                        //coffee1.Foreground = status == "3" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(169, 93, 8)) : status == "4" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 70, 22)) : new SolidColorBrush(System.Windows.Media.Color.FromRgb(216, 170, 118));
                    }
                    else if (id == 1)
                    {

                        coffeeName2.Text = name;
                        coffee2.Text = status == "3" ? "To be made" : status == "4" ? MainWindow.ismake ? "In Process" : "In Process" : "Completed";
                        // coffee2.Foreground = status == "3" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(169, 93, 8)) : status == "4" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 70, 22)) : new SolidColorBrush(System.Windows.Media.Color.FromRgb(216, 170, 118));

                    }
                    else if (id == 2)
                    {

                        coffeeName3.Text = name;
                        coffee3.Text = status == "3" ? "To be made" : status == "4" ? MainWindow.ismake ? "In Process" : "In Process" : "Completed";
                        //coffee3.Foreground = status == "3" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(169, 93, 8)) : status == "4" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 70, 22)) : new SolidColorBrush(System.Windows.Media.Color.FromRgb(216, 170, 118));
                    }
                    else if (id == 3)
                    {

                        coffeeName4.Text = name;
                        coffee4.Text = status == "3" ? "To be made" : status == "4" ? MainWindow.ismake ? "In Process" : "In Process" : "Completed";
                        //coffee4.Foreground = status == "3" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(169, 93, 8)) : status == "4" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 70, 22)) : new SolidColorBrush(System.Windows.Media.Color.FromRgb(216, 170, 118));
                    }
                    else if (id == 4)
                    {

                        coffeeName5.Text = name;
                        coffee5.Text = status == "3" ? "To be made" : status == "4" ? MainWindow.ismake ? "In Process" : "In Process" : "Completed";
                        //coffee5.Foreground = status == "3" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(169, 93, 8)) : status == "4" ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 70, 22)) : new SolidColorBrush(System.Windows.Media.Color.FromRgb(216, 170, 118));
                    }
                }));
                GC.Collect();
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 订单列表界面显示
        /// </summary>
        /// <param name="lists"></param>
        public void OrderShow(List<deviceOrderQueueList> lists)
        {
            try
            {
                OrderShowClear();
                this.Dispatcher.BeginInvoke(new Action(delegate
                {
                    if (lists.Count > 0)
                    {
                        NoName1.Text = "NO1:";
                        no1.Text = lists[0].orderNum;
                    }
                    if (lists.Count > 1)
                    {
                        NoName2.Text = "NO2:";
                        no2.Text = lists[1].orderNum;
                    }
                    if (lists.Count > 2)
                    {
                        NoName3.Text = "NO3:";
                        no3.Text = lists[2].orderNum;
                    }
                    if (lists.Count > 3)
                    {
                        NoName4.Text = "NO4:";
                        no4.Text = lists[3].orderNum;
                    }
                    if (lists.Count > 4)
                    {
                        NoName5.Text = "NO5:";
                        no5.Text = lists[4].orderNum;
                    }
                    if (lists.Count > 5)
                    {
                        NoName6.Text = "NO6:";
                        no6.Text = lists[5].orderNum;
                    }
                    if (lists.Count > 6)
                    {
                        NoName7.Text = "NO7:";
                        no7.Text = lists[6].orderNum;
                    }
                    if (lists.Count > 7)
                    {
                        NoName8.Text = "NO8:";
                        no8.Text = lists[7].orderNum;
                    }
                    if (lists.Count > 8)
                    {
                        NoName9.Text = "NO9:";
                        no9.Text = lists[8].orderNum;
                    }
                    if (lists.Count > 9)
                    {
                        NoName10.Text = "NO10:";
                        no10.Text = lists[9].orderNum;
                    }
                    Coffee1.Text = lists[0].orderNum.Substring(lists[0].orderNum.Length - 6, 6);
                    Console.WriteLine(lists[0].orderNum.Substring(lists[0].orderNum.Length - 6, 6));
                    type1.Text = ismake ? "In Process" : "In Process";// ismake ? "In Process" : "In Process";// ismake ? "In Process" : "In Process";
                }));
            }
            catch (Exception)
            {
                return;
            }
        }
        public void MessageShow()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        Device device = new Device();
                        string lists = JsonConvert.SerializeObject(device);
                        var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);
                        if (data != null)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
                                        {
                                            if (ddiD != list[0].id)
                                            {
                                                grid1.Visibility = Visibility.Visible;
                                                grid2.Visibility = Visibility.Collapsed;
                                                ddiD = list[0].id;
                                                OrderShowClear();
                                                CoffeeMsgClear();
                                            }
                                            if (list[0].typeList2.Count > 0)
                                            {
                                                OrderShow(list);
                                                if (list[0].typeList2.Count > 0)
                                                {
                                                    for (int i = 0; i < list[0].typeList2.Count; i++)
                                                    {
                                                        CoffeeTypeShow(list[0].typeList2[i].id, i, list[0].typeList2[i].language == "zh" ? list[0].typeList2[i].goodsName : list[0].typeList2[i].language == "en" ? list[0].typeList2[i].goodsNameEn : list[0].typeList2[i].goodsNameOt, list[0].typeList2[i].status, list[0].typeList2[i].goodsOptionName, list[0].typeList2[i].price, list[0].typeList2[i].goodsPath);
                                                    }

                                                }
                                            }
                                        }));
                                    }
                                    else
                                    {
                                        this.Dispatcher.BeginInvoke(new Action(delegate
                                        {
                                            OrderShowClear();
                                            CoffeeMsgClear();
                                            grid1.Visibility = Visibility.Collapsed;
                                            grid2.Visibility = Visibility.Visible;
                                        }));
                                    }
                                }
                                else
                                {
                                    this.Dispatcher.BeginInvoke(new Action(delegate
                                    {
                                        grid1.Visibility = Visibility.Collapsed;
                                        grid2.Visibility = Visibility.Visible;
                                        OrderShowClear();
                                        CoffeeMsgClear();
                                    }));
                                }
                            }
                        }
                        else
                        {
                            this.Dispatcher.BeginInvoke(new Action(delegate
                            {
                                grid1.Visibility = Visibility.Collapsed;
                                grid2.Visibility = Visibility.Visible;
                                OrderShowClear();
                                CoffeeMsgClear();
                            }));
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(2000);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {

            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 所有订单内容显示
        /// </summary>
        public void OrderShowClear()
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(delegate
                {
                    no1.Text = string.Empty;
                    NoName1.Text = string.Empty;
                    no2.Text = string.Empty;
                    NoName2.Text = string.Empty;
                    no3.Text = string.Empty;
                    NoName3.Text = string.Empty;
                    no4.Text = string.Empty;
                    NoName4.Text = string.Empty;
                    no5.Text = string.Empty;
                    NoName5.Text = string.Empty;
                    no6.Text = string.Empty;
                    NoName6.Text = string.Empty;
                    no7.Text = string.Empty;
                    NoName7.Text = string.Empty;
                    no8.Text = string.Empty;
                    NoName8.Text = string.Empty;
                    no9.Text = string.Empty;
                    NoName9.Text = string.Empty;
                    no10.Text = string.Empty;
                    NoName10.Text = string.Empty;

                }));
                GC.Collect();
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 第一个订单得订单详情清空界面咖啡
        /// </summary>
        public void CoffeeMsgClear()
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(delegate
                {
                    coffeeName1.Text = "";
                    coffeeName2.Text = "";
                    coffeeName3.Text = "";
                    coffeeName4.Text = "";
                    coffeeName5.Text = "";
                    coffee1.Text = "";
                    coffee2.Text = "";
                    coffee3.Text = "";
                    coffee4.Text = "";
                    coffee5.Text = "";
                }));
                GC.Collect();
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void media_Loaded(object sender, RoutedEventArgs e)
        {
            ((MediaElement)sender).Play();
            ((MediaElement)sender).Volume = 0f;
        }
        private void media_MediaEnded(object sender, RoutedEventArgs e)
        {
            // MediaElement需要先停止播放才能再开始播放，
            // 否则会停在最后一帧不动
            ((MediaElement)sender).Stop();
            ((MediaElement)sender).Play();
        }
        private void media_Unloaded(object sender, RoutedEventArgs e)
        {
            ((MediaElement)sender).Stop();
        }

        private void exit_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
                {
                    MessageBoxResult result = MessageBox.Show("Exit orders will not be made,Do you really want to quit?", "Exit", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        this.Dispatcher.BeginInvoke(new Action(delegate
                        {
                            System.Windows.Application.Current.Shutdown();
                        }));
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
